﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GW_SELECT_LEVEL : MonoBehaviour
{
	public static GW_SELECT_LEVEL instance;
	int _levelPage;
	int maxPage = 3;

	public int levelPage {
		get { return _levelPage; }
		set {
			if (value <= maxPage && value >= 0) {
				_levelPage = value;
				for (int i = 0; i < listLevelButtons.Count; i++) {
					listLevelButtons [i].InitLevel ();
				}
			}
		}
	}

	void Awake ()
	{
		instance = this;
	}

	List<LevelButton> listLevelButtons;
	public GameObject levelButtonPrefap;
	public GameObject levelGroup;

	void OnEnable ()
	{
		if (AudioManager.instance.AS_MUSIC.clip != AudioManager.instance.GetAudioClip (AudioClipType.BGM_TItle)) {
			AudioManager.instance.PlayMusic (AudioClipType.BGM_TItle);
		}
		if (instance != null && listLevelButtons != null) {
			GameManager.gameState = GameState.SELECT_LEVEL;
			for (int i = 0; i < listLevelButtons.Count; i++) {
				listLevelButtons [i].InitLevel ();
			}
		}
	}

	void Start ()
	{
		listLevelButtons = new List<LevelButton> ();
		for (int i = 0; i < 16; i++) {
			GameObject lvObj = (GameObject)Instantiate (levelButtonPrefap, levelGroup.transform);
			LevelButton lv = lvObj.GetComponent<LevelButton> ();
			lv.id = i + 1;
			listLevelButtons.Add (lv);
		}
		levelPage = 0;
	}

	public void NextPage ()
	{
		levelPage++;
	}

	public void BackPage ()
	{
		levelPage--;
	}

	public void BackToMenu  ()
	{
		GameManager.gameState = GameState.MENU;
		gameObject.SetActive (false);
		WindowManager.OpenWindow (WindowName.GW_MENU);
	}

}
